<%--

 Controls WebQuilt proxied / non-proxied page view synchronization.
 
 This software is distributed under the Berkeley Software License.
   See "http://guir.cs.berkeley.edu/projects/COPYRIGHT.txt" for details

 author: Jeffrey Heer 2/01 - jheer@hkn.eecs.berkeley.edu

--%>
<%@ page import="java.io.*"                                                      %>
<%@ page import="java.net.URL"                                                   %>
<%@ page import="edu.berkeley.guir.webquilt.*"                                   %>
<%@ page import="edu.berkeley.guir.webquilt.proxy.*"                             %>
<%@ page import="HTTPClient.WebQuiltURLConnection"                               %>
<%
       ProxyEdit proxyEdit = (ProxyEdit)session.getAttribute("EDITOR");
       if ( proxyEdit == null ) { 
	   %>
              <html>
	      <head>
		   <title>error...</title>
              </head>
              <body bgcolor="#FFFFFF">
		   <h2>Internal Error</h2>
		   File sync.jsp could not find the requested user session.
	      </body>
	      </html>
	   <%
	   return;
       }

       String strSync  = (String)request.getParameter("sync");
       String strTid   = (String)request.getParameter("tid");

       if ( "false".equals(strSync) ) {
	   proxyEdit.setSync(false);
	   %>
	       <html>
               <head>
		    <title>disabling sync...</title>
	       </head>
	       <body onLoad='window.close()'></body>
	       </html>
           <%
       } else {

	   if ( strTid == null ) { 
	       %>
                   <html>
		   <head>
			<title>error...</title>
		   </head>
                   <body bgcolor="#FFFFFF">
			<h2>Error</h2>
			No destination page was specified to sync.jsp.
		   </body>
	           </html>
	       <%
	       return;
	   }

	   if ( "true".equals(strSync) ) {
	       proxyEdit.setSync(true);
	   }

	   try {
	       String           strTaskID   = (String)session.getAttribute("TASKID");
	       String           strUserID   = (String)session.getAttribute("USERID");
	       String           strBaseName = strTaskID + "/" + strTaskID + "-" + strUserID;
	       String           strFile     = WebQuiltLib.getLogDir() +
		                               strBaseName + "/" + strTid + ".html";
	       FileInputStream  istream = new FileInputStream(strFile);
	       OutputStream     ostream = response.getOutputStream();

	       WebQuiltLib.connectStreams(istream, ostream);

	   } catch ( Exception e ) {
	       e.printStackTrace();
	   }

       }
	   
%>

